package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Models.AdditiveNotes;
import Models.BoilHopNotes;
import Models.BrewNote;
import Models.Equipment;
import Models.MaltNotes;
import Models.MashNotes;
import Models.SaltNotes;

public class BrewNotePresenter extends AbstractPresenter<BrewNote> {
	final String SINGLEITEM = "brewNotes";
	final String MULTIITEM = "brewNotess";
	
	public ArrayList<BrewNote> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public BrewNote tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public BrewNote createItemFromJSONObject(JSONObject jsonObject){
		BrewNote item = new BrewNote();
		try{
			item.setBoilpH(jsonObject.getDouble("boilpH"));
			item.setBoilStartTime(timeFormat.parse(jsonObject.getString("boilStart")));
			item.setBoilStopTime(timeFormat.parse(jsonObject.getString("boilStop")));
			item.setBrewNoteID(jsonObject.getInt("bnid"));
			item.setEmpID(jsonObject.getInt("employeeID"));
			item.setFinalVolume(jsonObject.getDouble("volFinal"));
			item.setFlowRate(jsonObject.getDouble("flowrate"));
			item.setHydrationStartTime(timeFormat.parse(jsonObject.getString("hydStart")));
			item.setHydrationStopTime(timeFormat.parse(jsonObject.getString("hydStop")));
			item.setHydrationTemp(jsonObject.getDouble("hydTemp"));
			item.setKnockOutStopTime(timeFormat.parse(jsonObject.getString("KOStop")));
			item.setKnockOutTemp(jsonObject.getDouble("KOTemp"));
			item.setMashpH(jsonObject.getDouble("mashpH"));
			item.setOriginalGravity(jsonObject.getDouble("bog"));
			item.setOxygenationCFM(jsonObject.getDouble("BOxy"));
			item.setPreBoilBrix(jsonObject.getDouble("PBBrix"));
			item.setPreBoilVolume(jsonObject.getDouble("PBVol"));
			item.setRestStopTime(timeFormat.parse(jsonObject.getString("restStop")));
			item.setSpargeBrix(jsonObject.getDouble("spargeBrix"));
			item.setSpargepH(jsonObject.getDouble("spargepH"));
			item.setVorlauftNotes(jsonObject.getString("vorNotes"));
			item.setVorlauftStartTime(timeFormat.parse(jsonObject.getString("vorStart")));
			item.setVorlauftStopTime(timeFormat.parse(jsonObject.getString("vorStop")));
			item.setWhirlPoolStopTime(timeFormat.parse(jsonObject.getString("whirlStop")));
			
			//Set Documents Collection
			ArrayList<Equipment> equipment = new ArrayList<Equipment>();
			if(!jsonObject.isNull("equipmentCollection")){
				Object itm = jsonObject.get("equipmentCollection");
				EquipmentPresenter presenter = new EquipmentPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						equipment.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					equipment.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setEquipment(equipment);
			}
			
			//Set Documents Collection
			ArrayList<MashNotes> mashNotes = new ArrayList<MashNotes>();
			if(!jsonObject.isNull("mashNoteCollection")){
				Object itm = jsonObject.get("mashNoteCollection");
				MashNotesPresenter presenter = new MashNotesPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						mashNotes.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					mashNotes.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setMashNoteCollection(mashNotes);
			}
			
			//Set Documents Collection
			ArrayList<MaltNotes> maltNotes = new ArrayList<MaltNotes>();
			if(!jsonObject.isNull("maltNoteCollection")){
				Object itm = jsonObject.get("maltNoteCollection");
				MaltNotesPresenter presenter = new MaltNotesPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						maltNotes.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					maltNotes.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setMaltNotes(maltNotes);
			}
			
			//Set Documents Collection
			ArrayList<BoilHopNotes> boilHops = new ArrayList<BoilHopNotes>();
			if(!jsonObject.isNull("boilHopNoteCollection")){
				Object itm = jsonObject.get("boilHopNoteCollection");
				BoilHopNotesPresenter presenter = new BoilHopNotesPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						boilHops.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					boilHops.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setBoilHopNotes(boilHops);
			}
			
			//Set Documents Collection
			ArrayList<AdditiveNotes> additives = new ArrayList<AdditiveNotes>();
			if(!jsonObject.isNull("additiveNoteCollection")){
				Object itm = jsonObject.get("additiveNoteCollection");
				AdditiveNotesPresenter presenter = new AdditiveNotesPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						additives.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					additives.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setAdditiveNotes(additives);
			}
			
			//Set Documents Collection
			ArrayList<SaltNotes> salts = new ArrayList<SaltNotes>();
			if(!jsonObject.isNull("saltNoteCollection")){
				Object itm = jsonObject.get("saltNoteCollection");
				SaltNotesPresenter presenter = new SaltNotesPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						salts.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					salts.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setSaltNotes(salts);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(BrewNote item){
		try{
			JSONObject json = new JSONObject();

			json.put("boilpH", item.getBoilpH());
			json.put("boilStart", getDate(item.getBoilStartTime()));
			json.put("boilStop", getDate(item.getBoilStopTime()));
			json.put("bnid", item.getBrewNoteID());
			json.put("employeeID", item.getEmpID());
			json.put("volFinal", item.getFinalVolume());
			json.put("flowrate", item.getFlowRate());
			json.put("hydStart", getDate(item.getHydrationStartTime()));
			json.put("hydStop", getDate(item.getHydrationStopTime()));
			json.put("hydTemp", item.getHydrationTemp());
			json.put("KOStop", getDate(item.getKnockOutStopTime()));
			json.put("KOTemp", item.getKnockOutTemp());
			json.put("mashpH", item.getMashpH());
			json.put("bog", item.getOriginalGravity());
			json.put("BOxy", item.getOxygenationCFM());
			json.put("PBBrix", item.getPreBoilBrix());
			json.put("PBVol", item.getPreBoilVolume());
			json.put("restStop", getDate(item.getRestStopTime()));
			json.put("spargeBrix", item.getSpargeBrix());
			json.put("spargepH", item.getSpargepH());
			json.put("vorNotes", item.getVorlauftNotes());
			json.put("vorStart", getDate(item.getVorlauftStartTime()));
			json.put("vorStop", getDate(item.getVorlauftStopTime()));
			json.put("whirlStop", getDate(item.getWhirlPoolStopTime()));
			
			//Create Doc Collection
			if(item.getEquipment()!=null){
				EquipmentPresenter presenter = new EquipmentPresenter();
				if(item.getEquipment().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getEquipment().size(); i++){
						jsonArray.put(presenter.createJSON(item.getEquipment().get(i)));
					}
					json.put("equipmentCollection", jsonArray);
				}
			}
			
			//Create Doc Collection
			if(item.getMashNoteCollection()!=null){
				MashNotesPresenter presenter = new MashNotesPresenter();
				if(item.getMashNoteCollection().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getMashNoteCollection().size(); i++){
						jsonArray.put(presenter.createJSON(item.getMashNoteCollection().get(i)));
					}
					json.put("mashNoteCollection", jsonArray);
				}
			}
			
			//Create Doc Collection
			if(item.getMaltNotes()!=null){
				MaltNotesPresenter presenter = new MaltNotesPresenter();
				if(item.getMaltNotes().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getMaltNotes().size(); i++){
						jsonArray.put(presenter.createJSON(item.getMaltNotes().get(i)));
					}
					json.put("maltNoteCollection", jsonArray);
				}
			}
			
			//Create Doc Collection
			if(item.getBoilHopNotes()!=null){
				BoilHopNotesPresenter presenter = new BoilHopNotesPresenter();
				if(item.getBoilHopNotes().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getBoilHopNotes().size(); i++){
						jsonArray.put(presenter.createJSON(item.getBoilHopNotes().get(i)));
					}
					json.put("boilHopNoteCollection", jsonArray);
				}
			}
			
			//Create Doc Collection
			if(item.getAdditiveNotes()!=null){
				AdditiveNotesPresenter presenter = new AdditiveNotesPresenter();
				if(item.getAdditiveNotes().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getAdditiveNotes().size(); i++){
						jsonArray.put(presenter.createJSON(item.getAdditiveNotes().get(i)));
					}
					json.put("additiveNoteCollection", jsonArray);
				}
			}
			
			//Create Doc Collection
			if(item.getSaltNotes()!=null){
				SaltNotesPresenter presenter = new SaltNotesPresenter();
				if(item.getSaltNotes().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getSaltNotes().size(); i++){
						jsonArray.put(presenter.createJSON(item.getSaltNotes().get(i)));
					}
					json.put("saltNoteCollection", jsonArray);
				}
			}
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public BrewNote getEmptyEntity(){
		BrewNote entity = new BrewNote();
		return entity;
	}
}
